class UserSessionsController < ApplicationController
  before_filter :require_no_user, :only => [:new, :create]
  before_filter :require_user, :only => :destroy

  layout 'users'

  def new
    @user_session = UserSession.new
  end
  
  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = "Logowanie zakończone powodzeniem!"
      user = User.find_by_email(@user_session.email)
      user.login!
      if user.admin?
        redirect_to :controller => 'admin'
      else
        redirect_to :controller => 'users'
      end    
    else
      render :action => :new
    end  
  end

  def destroy
    current_user.logout!
    current_user_session.destroy
    flash[:notice] = "Wylogowano pomyślnie!"
    redirect_to :controller => 'users', :action => 'about'
  end
end
